package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.dao.cargo.ExtCproductDao;
import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.ExtCproduct;
import cn.itcast.domain.cargo.ExtCproductExample;
import cn.itcast.service.cargo.ExtCproductService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class ExtCproductServiceImpl implements ExtCproductService {

    @Autowired
    private ExtCproductDao extCproductDao;
    @Autowired
    private ContractDao contractDao;

    @Override
    public void save(ExtCproduct extCproduct) {

//1.设置id
//2.计算附件的总金额
//3.设置附件总金额
//4.保存附件
//5.根据id查询购销合同
//6.设置购销合同的总金额
//7.设置附件数量
//8.更新合同


//        计算附件的小计金额
       Double amount =  extCproduct.getCnumber()*extCproduct.getPrice();
       extCproduct.setAmount(amount);
//        修改购销合同上的附件数和总金额
//        查询购销合同

        Contract contract = contractDao.selectByPrimaryKey(extCproduct.getContractId());
        contract.setExtNum(contract.getExtNum()+1);
        contract.setTotalAmount(contract.getTotalAmount()+amount);
        contractDao.updateByPrimaryKeySelective(contract);


        extCproductDao.insertSelective(extCproduct);
    }

    @Override
    public void update(ExtCproduct extCproduct) {
//        对购销合同总金额的影响
//        重新计算新的小计金额
       Double amount_new = extCproduct.getPrice()*extCproduct.getCnumber();
       extCproduct.setAmount(amount_new);

        ExtCproduct extCproduct_old = extCproductDao.selectByPrimaryKey(extCproduct.getId());
        Double amount_old = extCproduct_old.getAmount();
        Contract contract = contractDao.selectByPrimaryKey(extCproduct.getContractId());
        contract.setTotalAmount(contract.getTotalAmount()+amount_new-amount_old);
        contractDao.updateByPrimaryKeySelective(contract);

        extCproductDao.updateByPrimaryKeySelective(extCproduct);
    }

    @Override
    public void delete(String id) {
//        对购销合同总金额和附件数的影响
        ExtCproduct extCproduct_old = extCproductDao.selectByPrimaryKey(id);
        Double amount_old = extCproduct_old.getAmount();// 即将删除的附件的小计金额
        Contract contract = contractDao.selectByPrimaryKey(extCproduct_old.getContractId());
        contract.setTotalAmount(contract.getTotalAmount()-amount_old);
        contract.setExtNum(contract.getExtNum()-1);
        contractDao.updateByPrimaryKeySelective(contract);
        extCproductDao.deleteByPrimaryKey(id);
    }

    @Override
    public ExtCproduct findById(String id) {
        return extCproductDao.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo findAll(ExtCproductExample example, int page, int size) {
        PageHelper.startPage(page,size);
        List<ExtCproduct> list = extCproductDao.selectByExample(example);
        return new PageInfo(list);
    }
}
